// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    canIUseGetUserProfile: false,
    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName'), // 如需尝试获取用户信息可改为false
    msg: "初中古诗",
    num: 0,
    hotPoetImg: ['/assets/libai.png', '/assets/dufu.png', '/assets/baijuyi.png', '/assets/wangwei.png'],
    hotList: [],
    innerKind: ["小学古诗", "初中古诗", "高中古诗"],
    innerKindNum: 1,
    innerKindFlat: false,
    hotPoet: [],
    themeList: [],
    activeTar: 0
  },
  onChange(e) {
    console.log(e.detail);
    if (e.detail == 1) {
      wx.navigateTo({
        url: '../search/search',
      })
    } else if (e.detail == 2) {
      wx.switchTab({
        url: '../my/my',
      })
    }
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  changeKind() {
    this.setData({
      innerKindFlat: !this.data.innerKindFlat
    })
  },
  async changeKindNum(e) {
    if (e.currentTarget.dataset.id != this.data.innerKindNum) {
      this.setData({
        innerKindNum: e.currentTarget.dataset.id,
        innerKindFlat: false
      })
      await this.sendHot()
    }


  },
  onLoad() {
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
  },

  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
    
  },
  async changeNum(e) {
    console.log(e.currentTarget.dataset.id)
    this.setData({
      num: e.currentTarget.dataset.id
    })
    await this.sendHot()
  },
  sendHot() {
    let that = this;
    return new Promise((reslove, reject) => {
      if (this.data.num == 0) {
        wx.request({
          url: 'http://localhost:3000/sqlData/hotList',
          method: "post",
          data: {
            tags: that.data.innerKind[that.data.innerKindNum]
          },
          success(data) {
            data.data.rows.map((item) => {
              item.poet = JSON.parse(item.poet)
              item.tags = JSON.parse(item.tags)
            })
            that.setData({
              hotList: data.data.rows
            })
            console.log(that.data.hotList, 55)
          }
        })
        wx.hideLoading()

        reslove({
          code: 200,
          msg: "获取成功",

        })
      } else {
        wx.request({
          url: 'http://localhost:3000/sqlData/hotList2',
          method: "post",
          // innerKind[innerKindNum]
          success(data) {
            data.data.rows.map((item) => {
              item.poet = JSON.parse(item.poet)
              item.tags = JSON.parse(item.tags)
            })
            that.setData({
              hotList: data.data.rows
            })
          }
        })
        wx.hideLoading()

        reslove({
          code: 200,
          msg: "获取成功",

        })

      }
    })


  },
  getHotPoet() {
    let that = this
    wx.request({
      url: 'http://localhost:3000/poetry/poetHot',
      method: "POST",
      success(data) {
        // hotPoet
        that.setData({
          hotPoet: data.data.rows
        })
      }
    })
  },
  async onShow() {
    wx.showLoading({
      title: '加载中',
    })
    await this.sendHot()
    this.getHotPoet()
    this.getTheme()
  },
  jumpGO(e) {
    wx.setStorageSync('msg', e.currentTarget.dataset.msg)
    wx.setStorageSync('poetName', e.currentTarget.dataset.name)
    console.log(e.currentTarget.dataset.name)
    wx.navigateTo({
      url: '../detail/detail',
    })

  },
  jumpGOPoet(e) {
    wx.setStorageSync('poetName', e.currentTarget.dataset.msg)

    wx.navigateTo({
      url: `../particulars/particulars`,
    })
  },
  getTheme() {
    let that = this
    wx.request({
      url: 'http://localhost:3000/poetry/poetryThemeList',
      method: "POST",
      success(data) {
        console.log(data.data.rows, 4545)
        that.setData({
          themeList: data.data.rows,
        })
      }
    })
  },
  jumpGoTheme(e) {
    console.log(e.currentTarget.dataset.msg, 5656)
    wx.setStorageSync('theme', e.currentTarget.dataset.msg)
    wx.navigateTo({
      url: '../theme/theme',
    })
  }

})